package com.warm.demo.mf.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.warm.demo.mf.api.dto.TestTreeDto;
import com.warm.demo.mf.api.vo.TestTreeVo;
import com.warm.demo.mf.domain.TestTree;
import com.warm.demo.mf.mapper.TestTreeMapper;
import com.warm.demo.mf.service.ITestTreeService;
import com.warm.mf.core.service.impl.BaseServiceImpl;
import org.noear.solon.annotation.Component;

import java.util.Arrays;
import java.util.List;

import static com.warm.demo.mf.convert.TestTreeC.testTreeC;
import static com.warm.demo.mf.domain.table.TestTreeTableDef.test_tree;

/**
 * 测试树Service业务层处理
 *
 * @author warm
 * @date 2023-09-04
 */
@Component
public class TestTreeServiceImpl extends BaseServiceImpl<TestTreeMapper, TestTree> implements ITestTreeService {


    @Override
    public TestTreeVo selectById(Long id) {
        TestTree entity = getMapper().selectOneById(id);
        return testTreeC.convert(entity);
    }


    @Override
    public List<TestTreeVo> selectList(TestTreeDto dto) {
        return buildQuery(dto).listAs(TestTreeVo.class);
    }

    public QueryChain<TestTree> buildQuery(TestTreeDto dto) {
        return this.queryChain()
                .where(test_tree.parent_id.eq(dto.getParentId()))
                .and(test_tree.tree_name.like(dto.getTreeName()))
                .and(test_tree.version.eq(dto.getVersion()));
    }

    @Override
    public int insertByDto(TestTreeDto dto) {
        TestTree entity = testTreeC.convert(dto);
        return getMapper().insertSelective(entity);
    }

    @Override
    public int updateByDto(TestTreeDto dto) {
        TestTree entity = testTreeC.convert(dto);
        return getMapper().update(entity);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return getMapper().deleteBatchByIds(Arrays.asList(ids));
    }
}
